package edu.must.uh.servlet;

import java.util.List;
import java.util.Map;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.log4j.Logger;
import edu.must.uh.bean.DeployQueryResultBean;
import edu.must.uh.impl.AttendancdCodeImpl;
import edu.must.uh.impl.BaseUserExImpl;
import edu.must.uh.util.R;
import edu.must.uh.util.ServletUtils;
import edu.must.uh.util.Tools;
import edu.must.uh.util.ToolsOfString;

@WebServlet("/AttendancdCodeServlet")
public class AttendancdCodeServlet extends BaseServlet {
	
    private static final long serialVersionUID = 12197442526341123L;
  
    AttendancdCodeImpl dataImpl = new AttendancdCodeImpl();    
    public Object getList(HttpServletRequest request, HttpServletResponse response) throws Exception {  
        dataImpl._PageBean = super.pageBean;
        Map<String, Object> searchcondition = ServletUtils.getsearchcondition(request);
        DeployQueryResultBean resList = dataImpl.getLimitlist(searchcondition, " ORDER BY CODE ");                 
        for (Map<String, Object> map : resList.getResultList()) {
                   
            ServletUtils.createDateHelper(map,"ATTEND_DATE"); 
        }        
        return R.succ(resList.getResultList(),resList.getTotal());         
    }
    
 public Object getlistSelect(HttpServletRequest request, HttpServletResponse response) throws Exception {  
        List<Map<String, Object>> resList = dataImpl.getlistSelect(null," ORDER BY CODE");                 
        return R.succ(resList);         
    }
 
 
 public Object getActuCode(HttpServletRequest request, HttpServletResponse response) throws Exception {  
     Map<String, Object> searchcondition = ServletUtils.getsearchcondition(request);
     Map<String, Object> resMap = dataImpl.getActuCode(searchcondition,"");  
     if(resMap ==  null  ) {
         return R.fail(" NO DATA");
     }
     return R.succ(resMap);         
 }

    
    public Object getOneData(HttpServletRequest request, HttpServletResponse response) throws Exception { 
    	String keyValue =ToolsOfString.NulltoEmpty(request.getParameter("ID")); 
    	String sqlString = "SELECT  CODE, REMARK,ISNIGHT,HOURS,TYPE,PERIOD FROM attendance_code Where DELETEMARK = 0  ";
    	sqlString = sqlString + " AND ID = '"+keyValue+"'";
        Map<String,Object>  map = dataImpl.getMap(sqlString);       
        return R.succ(map);         
    }
    
    public Object delete(HttpServletRequest request, HttpServletResponse response) throws Exception {      
            
        String keyValue =ToolsOfString.NulltoEmpty(request.getParameter("ID")); 
        Integer resInteger = dataImpl.delete(keyValue);
        return R.succ(resInteger);         
    }
    
  public Object save(HttpServletRequest request, HttpServletResponse response) throws Exception {  
      		
		String sql = "";
		Map<String, Object> searchcondition = ServletUtils.getsearchcondition(request);
	    String ID =ToolsOfString.NulltoEmpty(searchcondition.get("ID"));
        String CODE =ToolsOfString.NulltoEmpty(searchcondition.get("CODE")); 
      
        String ISNIGHT =ToolsOfString.NulltoEmpty(searchcondition.get("ISNIGHT"));
        String HOURS =ToolsOfString.NulltoEmpty(searchcondition.get("HOURS"));
        String TYPE =ToolsOfString.NulltoEmpty(searchcondition.get("TYPE"));
        String PERIOD =ToolsOfString.NulltoEmpty(searchcondition.get("PERIOD"));
        
        String REMARK =ToolsOfString.NulltoEmpty(searchcondition.get("REMARK"));
        REMARK =  java.net.URLDecoder.decode(REMARK, "UTF-8");
       
        if(Tools.notEmpty(ID)) {
        	sql = "UPDATE attendance_code  SET CODE = '"+CODE+"',REMARK = '"+REMARK+"',ISNIGHT = '" +ISNIGHT+"',HOURS = '" +HOURS+"',TYPE = '" +TYPE+"',PERIOD = '" +PERIOD+"'";
        	sql = sql + " WHERE ID ='"+ID+"'"; 
        }else {
            ID = Tools.getUUID32();
        	sql = "INSERT INTO attendance_code(ID,CODE,REMARK,ISNIGHT,HOURS,TYPE,PERIOD)VALUES";
        	sql = sql + " ('"+ID+"','"+CODE+"','"+REMARK+"','"+ISNIGHT+"','"+HOURS+"','"+TYPE+"','"+PERIOD+"')"; 
        }
        Integer resInteger = dataImpl.save(sql);
        if(resInteger == 1) {
        	   return R.succ(); 
        }
        return R.fail();
       
    }
    
    
    
   
}
